﻿Imports System.Data
Imports System.Data.SqlClient
'Hacemos referencia al proyecto BusinessEntities
Imports BE = BusinessEntities

Public Class ClienteDAO
    Dim conexion As SqlConnection
    Dim comando As SqlDataAdapter
    Public Shared errores As String
    Dim com As SqlCommand

    'Instanciamos la clase Conexion
    Dim cn As New Conexion

    Public Function ListarClientes() As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_LISTARCLIENTES", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Dim ds As New DataSet
            comando.Fill(ds, "LosClientes")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function ListarNombreCompletoClientes() As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_LISTARNOMBRECOMPLETOCLIENTES", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Dim ds As New DataSet
            comando.Fill(ds, "NombreCompletoClientes")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function BuscarCliente(ByVal objeto As BE.ClienteBE) As DataSet
        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_BUSCARCLIENTE", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Definimos el paràmetro del stored procedure
            comando.SelectCommand.Parameters.Add _
            (New SqlParameter("@CODIGO", SqlDbType.Char, 6))

            comando.SelectCommand.Parameters("@CODIGO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@CODIGO").Value = objeto.codigo

            comando.Fill(ds, "ElCliente")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function ValidarCliente(ByVal objeto As BE.ClienteBE) As DataSet

        Dim ds As New DataSet
        Try
            conexion = cn.Conectar
            comando = New SqlDataAdapter("SP_VALIDARLOGIN", conexion)
            comando.SelectCommand.CommandType = CommandType.StoredProcedure

            'Definimos el paràmetro del stored procedure

            comando.SelectCommand.Parameters.Add(New SqlParameter("@USUARIO", SqlDbType.Char, 6))
            comando.SelectCommand.Parameters("@USUARIO").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@USUARIO").Value = objeto.usuario

            comando.SelectCommand.Parameters.Add(New SqlParameter("@PASSWORD", SqlDbType.Char, 6))
            comando.SelectCommand.Parameters("@PASSWORD").Direction = ParameterDirection.Input
            comando.SelectCommand.Parameters("@PASSWORD").Value = objeto.password


            'Dim ds As New DataSet
            comando.Fill(ds, "ClienteValidado")

        Catch ex As Exception
            errores = ex.Message
        Finally
            conexion.Dispose()
            comando.Dispose()
            ds.Dispose()
        End Try

        Return ds

    End Function

    Public Function InsertarCliente(ByVal objeto As BE.ClienteBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_INSERTARCLIENTE", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada
            com.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 70))
            com.Parameters("@NOMBRE").Direction = ParameterDirection.Input
            com.Parameters("@NOMBRE").Value = objeto.nombre

            com.Parameters.Add(New SqlParameter("@APELLIDO", SqlDbType.VarChar, 70))
            com.Parameters("@APELLIDO").Direction = ParameterDirection.Input
            com.Parameters("@APELLIDO").Value = objeto.apellido

            com.Parameters.Add(New SqlParameter("@EMAIL", SqlDbType.VarChar, 40))
            com.Parameters("@EMAIL").Direction = ParameterDirection.Input
            com.Parameters("@EMAIL").Value = objeto.email

            com.Parameters.Add(New SqlParameter("@DIRECCION", SqlDbType.VarChar, 70))
            com.Parameters("@DIRECCION").Direction = ParameterDirection.Input
            com.Parameters("@DIRECCION").Value = objeto.direccion

            com.Parameters.Add(New SqlParameter("@PAIS", SqlDbType.Char, 6))
            com.Parameters("@PAIS").Direction = ParameterDirection.Input
            com.Parameters("@PAIS").Value = objeto.pais

            com.Parameters.Add(New SqlParameter("@USUARIO", SqlDbType.Char, 6))
            com.Parameters("@USUARIO").Direction = ParameterDirection.Input
            com.Parameters("@USUARIO").Value = objeto.usuario

            com.Parameters.Add(New SqlParameter("@PASSWORD", SqlDbType.Char, 6))
            com.Parameters("@PASSWORD").Direction = ParameterDirection.Input
            com.Parameters("@PASSWORD").Value = objeto.password

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function


    Public Function ActualizarCliente(ByVal objeto As BE.ClienteBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_ACTUALIZARCLIENTE", conexion)
            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.Char, 6))
            com.Parameters("@CODIGO").Direction = ParameterDirection.Input
            com.Parameters("@CODIGO").Value = objeto.codigo


            com.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 70))
            com.Parameters("@NOMBRE").Direction = ParameterDirection.Input
            com.Parameters("@NOMBRE").Value = objeto.nombre

            com.Parameters.Add(New SqlParameter("@APELLIDO", SqlDbType.VarChar, 70))
            com.Parameters("@APELLIDO").Direction = ParameterDirection.Input
            com.Parameters("@APELLIDO").Value = objeto.apellido

            com.Parameters.Add(New SqlParameter("@EMAIL", SqlDbType.VarChar, 40))
            com.Parameters("@EMAIL").Direction = ParameterDirection.Input
            com.Parameters("@EMAIL").Value = objeto.email

            com.Parameters.Add(New SqlParameter("@DIRECCION", SqlDbType.VarChar, 70))
            com.Parameters("@DIRECCION").Direction = ParameterDirection.Input
            com.Parameters("@DIRECCION").Value = objeto.direccion

            com.Parameters.Add(New SqlParameter("@PAIS", SqlDbType.Char, 6))
            com.Parameters("@PAIS").Direction = ParameterDirection.Input
            com.Parameters("@PAIS").Value = objeto.pais

            com.Parameters.Add(New SqlParameter("@USUARIO", SqlDbType.Char, 6))
            com.Parameters("@USUARIO").Direction = ParameterDirection.Input
            com.Parameters("@USUARIO").Value = objeto.usuario

            com.Parameters.Add(New SqlParameter("@PASSWORD", SqlDbType.Char, 6))
            com.Parameters("@PASSWORD").Direction = ParameterDirection.Input
            com.Parameters("@PASSWORD").Value = objeto.password

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally

            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function

    Public Function EliminarCliente(ByVal objeto As BE.ClienteBE) As Boolean

        Try
            conexion = cn.Conectar
            com = New SqlCommand("SP_ELIMINARCLIENTE", conexion)

            com.CommandType = CommandType.StoredProcedure

            'Definimos los parámetros de entrada

            com.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.Char, 6))
            com.Parameters("@CODIGO").Direction = ParameterDirection.Input
            com.Parameters("@CODIGO").Value = objeto.codigo

            conexion.Open()
            com.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            errores = ex.Message
            Return False
        Finally
            If conexion.State = ConnectionState.Open Then conexion.Close()
            'Liberamos recursos
            conexion.Dispose()
            com.Dispose()
        End Try

    End Function


    Public Function ChapaError() As String
        Return errores
    End Function


End Class
